home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue72 / clinic / ReadXLSheet3U.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-06-11  |  1.4 KB  |  55 lines

  1. unit ReadXLSheet3U;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Grids, OleServer, Excel97;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     SG: TStringGrid;
  12.     XL: TExcelApplication;
  13.     XLSheet: TExcelWorksheet;
  14.     procedure FormCreate(Sender: TObject);
  15.   end;
  16.  
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21.  
  22. {$R *.DFM}
  23.  
  24. procedure TForm1.FormCreate(Sender: TObject);
  25. var
  26.   X, Y: Integer;
  27.   Range: Variant;
  28. const
  29.   DefLocale = 0; //Default locale value
  30. begin
  31.   XL.Workbooks.Open('c:\Versions.xls', EmptyParam, EmptyParam,
  32.     EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
  33.     EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
  34.     DefLocale);
  35.   XLSheet.ConnectTo(XL.ActiveSheet as _WorkSheet);
  36.   //Select last cell
  37.   XLSheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
  38.   //Set string grid size
  39.   SG.ColCount := XL.ActiveCell.Column;
  40.   SG.RowCount := XL.ActiveCell.Row;
  41.   //Read all cells from top left to last cell as a Variant array
  42.   Range := XL.Range['A1', XL.Cells.Item[SG.RowCount, SG.ColCount]].Value;
  43.   XL.Quit;
  44.   XLSheet.Disconnect;
  45.   XL.Disconnect;
  46.   //String grid cells are numbered from 0
  47.   //XL cells are numbered from 1
  48.   //Also, XL cells are indexed as (row, column), not (column, row)
  49.   for X := 0 to SG.ColCount - 1 do
  50.     for Y := 0 to SG.RowCount - 1 do
  51.       SG.Cells[X, Y] := Range[Y + 1, X + 1];
  52. end;
  53.  
  54. end.
  55.